<?php 
    session_start(); 
    if (!isset($_SESSION['user'])) {
        header("Location: login.php");
        exit;
    }

    include("../service/categoryService.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Chi Tiết Loại Sản Phẩm</title>
	<link rel="stylesheet" href="../css/admin-style.css" type="text/css" media="all" />
        <script type="text/javascript" src="../js/jquery-1.6.4.min.js"></script>
        <script type="text/javascript">
            function hideMessage() {
                var ok = document.getElementById("msgOK");
                addClass(ok, 'msg-hidden');
            }

            function addClass(el, name){
                el.className += (el.className ? ' ' : '') +name;
            }
            
            function editCategorySubmit(){
                var form = document.getElementById("form");
                form.method = "post";
                form.action = "../action/editCategoryAction.php";
                form.submit();
            }
            
            function addCategorySubmit(){
                var form = document.getElementById("form");
                form.method = "post";
                form.action = "../action/addCategoryAction.php";
                form.submit();
            }
            
            function validateForm(){
                var name = document.getElementById('name').value;
                var isValid = false;
                    if(name != ''){
                        
                        $.ajax({
                            type: "GET",
                            url: "../action/validateCategoryAction.php", 
                            data: "name=" + $("#name").val().trim(),
                            success:function(value){
                                if(value == "true"){
                                    
                                    $("#nameValidator").html("Đã tồn tại");
                                    isValid = false;
                                }else {
                                    $("#nameValidator").html("");
                                    isValid = true;
                                }
                            }
                        });
                    } else {
                        $('#nameValidator').html('Bắt buộc nhập');
                        isValid = false;
                    }
                return isValid;
            }
            
            $(document).ready(function(){
                $("#name").blur(function(){
                    validateForm();
                });
            });
        </script>
</head>
<body>
<!-- Header -->
<?php 
    include('header.php'); 
    
    /* process message */
    $actionResult = $_GET["actionResult"];
    $msg = "";
    
    if(isset($actionResult)){
        if($actionResult == "ok"){
            $msg = "Cập nhật thành công!";
            $class = "msg msg-ok";
        } elseif ($actionResult == "no") {
            $msg = "Không thể cập sản phẩm";
            $class = "msg msg-error";
        } elseif ($actionResult == "noadd"){
            $msg = "Không thể tạo sản phẩm";
            $class = "msg msg-error";
        }
    }
    
    if ($msg == '') $class = 'msg msg-ok msg-hidden';
?>
<!-- End Header -->

<!-- Container -->
<div id="container">
    <div class="shell">
        <!-- End Small Nav -->

        <!-- Message OK -->		
        <div class="<?php echo $class; ?>" id="msgOK">
            <p><strong><?php echo $msg; ?></strong></p>
            <a href="#" class="close" onclick="hideMessage();">close</a>
        </div>
        <!-- End Message OK -->	

        <br />
            <!-- Main -->
        <div id="main">
        <div class="cl">&nbsp;</div>

        <!-- Content -->
        <div id="content">        
            <!-- Box -->
            <div class="box">
                <!-- Box Head -->
                <div class="box-head">
                        <h2>Thêm Loại Sản Phẩm</h2>
                </div>
                <!-- End Box Head -->

                <form id="form" action="post" onsubmit="return validateForm();">
                    <!-- Form -->
                    <div class="form">
                        <p>
                            <label>Tên Loại Sản Phẩm</label>
                            <?php
                                if(isset($_GET["catID"])){
                                    $catID = $_GET["catID"];
                                    $category = getCategoryByID($catID);
                                }
                            ?>
                            <input type="text" class="field size1" value="<?php echo $category["CAT_NAME"]; ?>" id="name" name="name"/>
                            <input type="hidden" name="catID" value="<?php echo $category["CAT_ID"]; ?>"/>
                            <span id="nameValidator" style="color:red;"></span>
                        </p>
                        <p>
                            <label>Tên Loại Sản Phẩm Cha</label>
                            <select class="field size1" name="parentID">
                                <option value="">Không có cha</option>
                                <?php
                                    $result = getRootCategory();
                                    while($row = ConnectionManager::getInstance()->fetchArray()){
                                        if($category["CAT_CAT_ID"] == $row["CAT_ID"])
                                            echo "<option value='".$row["CAT_ID"]."' selected='selected'>".$row["CAT_NAME"]."</option>";
                                        else
                                            echo "<option value='".$row["CAT_ID"]."'>".$row["CAT_NAME"]."</option>";
                                    }
                                ?>
                                
                            </select>
                        </p>								
                        <p>
                            <label>Miêu tả </label>
                            <textarea class="field size1" rows="10" cols="30" name="description"><?php echo $category["CAT_DESCRIPTION"]; ?></textarea>
                        </p>	
                    </div>
                    <!-- End Form -->

                    <!-- Form Buttons -->
                    <div class="buttons">
                        <?php
                            if(isset($_GET["catID"])){
                        ?>
                            <input type="button" class="button" value="Chỉnh Sửa" id="btnEdit" name="btnEdit" onclick="editCategorySubmit();"/>
                        <?php
                            } else {
                        ?>
                                <input type="button" class="button" value="Thêm" id="btnAdd" name="btnAdd" onclick="addCategorySubmit();"/>
                        <?php
                            }
                        ?>
                        <input type="button" class="button" value="Trở Về" onclick="window.location = 'category.php'" />
                    </div>
                    <!-- End Form Buttons -->
                </form>
            </div>
            <!-- End Box -->
        </div>
        <!-- End Content -->
        <div class="cl">&nbsp;</div>			
        </div>
        <!-- Main -->
    </div>
</div>
<!-- End Container -->

<!-- Footer -->
<div id="footer">
    <?php include("footer.php");?>
</div>
<!-- End Footer -->
	
</body>
</html>
